package com.example.userp.app.myapplication.RcycleView.flexibledivider;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.annotation.DimenRes;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

/**
 * Created by yqritc on 2015/01/15.
 */
public class VerticalDividerItemDecoration extends FlexibleDividerDecoration {

    private MarginProvider mMarginProvider;

    protected VerticalDividerItemDecoration(Builder builder) {
        super(builder);
        mMarginProvider = builder.mMarginProvider;
        setDividerType();
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {

        RecyclerView.Adapter adapter = parent.getAdapter();
        int itemCount = adapter.getItemCount();
        int childCount = parent.getChildCount();
        int height = parent.getHeight();
        int width = parent.getWidth();
        GridLayoutManager gridLayoutManager = (GridLayoutManager) parent.getLayoutManager();
        int maxheight = height - parent.getPaddingTop();
        int minheight = parent.getPaddingBottom();
        int maxWidth = parent.getPaddingLeft();
        int minWidth = width - parent.getPaddingRight() ;

        int spanCount = gridLayoutManager.getSpanCount();
        for (int i = 0; i < childCount; i++) {
            View childAt = parent.getChildAt(i);
            int childAdapterPosition = parent.getChildAdapterPosition(childAt);
//            if (childAdapterPosition != 0) {
//                continue;
//            }
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) childAt.getLayoutParams();
            int dividerSize = getDividerSize(childAdapterPosition, parent);
            //行 线
            int v_left = childAt.getLeft() - dividerSize;
            v_left = v_left > maxWidth ? maxWidth : v_left;
            v_left = v_left < minWidth ? minWidth : v_left;

            int v_top = childAt.getTop() - dividerSize;
            v_top = v_top > maxheight ? maxheight : v_top;
            v_top = v_top < minheight ? minheight : v_top;

            int v_right = childAt.getRight() + dividerSize;
//            v_right = v_right > maxWidth ? maxWidth : v_right;
//            v_right = v_right < minWidth ? minWidth : v_right;

            int v_bottom = childAt.getTop();
            v_bottom = v_bottom > maxheight ? maxheight : v_bottom;
            v_bottom = v_bottom < minheight ? minheight : v_bottom;

//            if (v_top >= parent.getPaddingTop() - dividerSize && v_bottom <= height - parent.getPaddingBottom()) {
            if (childAdapterPosition <= spanCount - 1) {
                if (mShowFirstDivider) {
                    c.drawRect(v_left, v_top, v_right, v_bottom, mPaint);
                }
            } else {
                c.drawRect(v_left, v_top, v_right, v_bottom, mPaint);
            }
            if (childAdapterPosition == itemCount - 1 && mShowLastDivider && childAt.getBottom() <= height - parent.getPaddingBottom()) {
                c.drawRect(v_left, childAt.getBottom(), v_right, childAt.getBottom() + dividerSize, mPaint);
            }
//            }

            //竖 线
            int h_top = childAt.getTop() - dividerSize;
            h_top = h_top > maxheight ? maxheight : h_top;
            h_top = h_top < minheight ? minheight : h_top;

            int h_bottom = childAt.getBottom() + dividerSize;
            h_bottom = h_bottom > maxheight ? maxheight : h_bottom;
            h_bottom = h_bottom < minheight ? minheight : h_bottom;

            int h_left = childAt.getLeft() - dividerSize;
            h_left = h_left > maxWidth ? maxWidth : h_left;
            h_left = h_left < minWidth ? minWidth : h_left;
            int h_right = childAt.getLeft();
            h_right = h_right > maxWidth ? maxWidth : h_right;
            h_right = h_right < minWidth ? minWidth : h_right;
//            if (h_top >= layoutParams.topMargin + dividerSize && h_bottom >= layoutParams.bottomMargin + dividerSize) {
//                Log.e("TAG", "onDraw: " + childAdapterPosition % spanCount);
            if (childAdapterPosition % spanCount == spanCount - 1) {
                if (mShowRightDivider) {
                    c.drawRect(h_left, h_top, h_right, h_bottom, mPaint);
                }
            } else {
                c.drawRect(h_left, h_top, h_right, h_bottom, mPaint);
            }

            if (mShowLastDivider) {
                c.drawRect(childAt.getLeft() - dividerSize, h_top, childAt.getLeft(), h_bottom, mPaint);
            }
//            }
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        RecyclerView.Adapter adapter = parent.getAdapter();
        int itemCount = adapter.getItemCount();//获取个数
        int childAdapterPosition = parent.getChildAdapterPosition(view);//获取当前View的下标
        int dividerSize = getDividerSize(childAdapterPosition, parent);//获取 线 大小
        if (isLayoutManager(parent) == 0) {
            GridLayoutManager gridLayoutManager = (GridLayoutManager) parent.getLayoutManager();
            int spanCount = gridLayoutManager.getSpanCount();//获取一行的个数
            if (childAdapterPosition < spanCount && mShowFirstDivider) {//设置第一行 顶部
                outRect.top = dividerSize;
            } else {
                outRect.top = 0;
            }
            int mRemainder = itemCount % spanCount;//最后一行个数
            int remaining = mRemainder == 0 ? itemCount - spanCount : itemCount - mRemainder;//最后一行的第一个 下标

            if (childAdapterPosition < remaining) {//如果当前下标小于 最后一行下标
                outRect.bottom = dividerSize;
            } else {
                if (mShowLastDivider) {//限制最后一行
                    outRect.bottom = dividerSize;
                } else {
                    outRect.bottom = 0;
                }
            }

            if (childAdapterPosition % spanCount == 0 && mShowLeftDivider) {//最左边
                outRect.left = dividerSize;
            }

            if (childAdapterPosition % spanCount != spanCount - 1) {//最右边
                outRect.right = dividerSize;

            } else {
                if (mShowRightDivider) {
                    outRect.right = dividerSize;
                }
            }
        }
    }

    /**
     * Interface for controlling divider margin
     */
    public interface MarginProvider {

        /**
         * Returns top margin of divider.
         *
         * @param position Divider position (or group index for GridLayoutManager)
         * @param parent   RecyclerView
         * @return top margin
         */
        int dividerTopMargin(int position, RecyclerView parent);

        /**
         * Returns bottom margin of divider.
         *
         * @param position Divider position (or group index for GridLayoutManager)
         * @param parent   RecyclerView
         * @return bottom margin
         */
        int dividerBottomMargin(int position, RecyclerView parent);
    }

    public static class Builder extends FlexibleDividerDecoration.Builder<Builder> {

        private MarginProvider mMarginProvider = new MarginProvider() {
            @Override
            public int dividerTopMargin(int position, RecyclerView parent) {
                return 0;
            }

            @Override
            public int dividerBottomMargin(int position, RecyclerView parent) {
                return 0;
            }
        };

        public Builder(Context context) {
            super(context);
        }

        public Builder margin(final int topMargin, final int bottomMargin) {
            return marginProvider(new MarginProvider() {
                @Override
                public int dividerTopMargin(int position, RecyclerView parent) {
                    return topMargin;
                }

                @Override
                public int dividerBottomMargin(int position, RecyclerView parent) {
                    return bottomMargin;
                }
            });
        }

        public Builder margin(int verticalMargin) {
            return margin(verticalMargin, verticalMargin);
        }

        public Builder marginResId(@DimenRes int topMarginId, @DimenRes int bottomMarginId) {
            return margin(mResources.getDimensionPixelSize(topMarginId),
                    mResources.getDimensionPixelSize(bottomMarginId));
        }

        public Builder marginResId(@DimenRes int verticalMarginId) {
            return marginResId(verticalMarginId, verticalMarginId);
        }

        public Builder marginProvider(MarginProvider provider) {
            mMarginProvider = provider;
            return this;
        }

        public VerticalDividerItemDecoration build() {
            checkBuilderParams();
            return new VerticalDividerItemDecoration(this);
        }
    }

    public void setDividerType() {
        switch (mDividerType) {

            case COLOR:
                mPaint.setColor(mColorProvider.dividerColor(0, null));
                mPaint.setStrokeWidth(mSizeProvider.dividerSize(0, null));
                break;
        }
    }

}